import requests
from tkinter import *
import tkinter


# 百度地图API搜索
def baidu_search(query, region):
    url = 'http://api.map.baidu.com/place/v2/search?'
    output = 'json'
    ak = '自己的ak密钥'
    uri = url + 'query=' + query + '&region=' + region + '&output=' + output + '&ak=' + ak
    r = requests.get(uri)
    response_dict = r.json()
    results = response_dict["results"]
    with open('店铺信息.txt', 'w+', encoding='utf-8', newline='') as writ:
        for adr in results:
            if adr['province'] != '江苏省':
                writ.writelines('程序仅供江苏省使用，如果误报，请联系开发者！')
                break
            else:
                name = adr['name']
                location = adr['location']
                lng = float(location['lng'])
                lat = float(location['lat'])
                address = adr['address']
                if 'telephone' not in adr:
                    continue
                else:
                    telephone = adr['telephone']
                    dataname = '名称：' + name
                    dataloc = '坐标：%f,%f' % (lat, lng)
                    dizhi = '地址：' + address
                    iphone = '电话：' + telephone
                    writ.writelines('{}\n{}\n{}\n{}\n\n'.format(dataname, dataloc, dizhi, iphone))


def chaxun():
    city = bjk.get()
    baidu_search('汽车维修 汽车配件 汽车润滑油 汽车轮胎 汽车电瓶 汽车保养 汽车内饰', city)


if __name__ == '__main__':
    root = Tk()
    root.title('江苏省汽车服务查询    by QQ:761791827')
    root.geometry('400x200')
    Label(root, text='输入你要获取信息的城市：', width=20).grid(row=1, column=0, sticky=N + S + W)
    bjk = Entry(root, text='0', width=20)
    bjk.grid(row=1, column=1)
    butt = Button(root, text='开始查询', command=chaxun)
    butt.grid(row=1, column=2, sticky=W)

    root.mainloop()
